package com.mxx.common.core;

/**
 * @author: mxx
 * @date: 2021/8/19
 * @time: 13:51
 * @Description: 回归计数器 ,当计数数量达到回归数量时，会将计数器置零
 */
public class ResetCounter {
    private volatile Integer counter;
    private volatile Integer resetNumber;

    public ResetCounter(Integer resetNumber) {
        this.resetNumber = resetNumber;
        this.counter = 0;
    }

    public synchronized Integer incrementAndGet() {
        int result = ++counter;
        if (counter >= resetNumber) {
            counter = 0;
        }
        return result;

    }


    public int current() {
        return this.counter;
    }
}